//
//  MCImageScrollView.m
//  QuKeClient
//
//  Created by delicious on 2017/12/6.
//  Copyright © 2017年 delicious. All rights reserved.
//

#import "MCImageScrollView.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>
@interface UICollectionViewCell (update)
-(void)update:(id)model collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

@end


@implementation UICollectionViewCell (update)
-(void)update:(id)model collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}
@end




@implementation imageViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *imageView = ({
            UIImageView*l=[ UIImageView new];
            l.cornerRadius = frame.size.height/2;
            l.masksToBounds = YES;
           [self addSubview:l];
            l;
        });
        _imageView =imageView;
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
    }
    return self;
}
-(void)update:(id)model collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([model isKindOfClass:[NSURL class]]) {
        [self.imageView sd_setImageWithURL:model placeholderImage:IMAGE(@"myICon") options:SDWebImageRefreshCached];
    }
    else if([model isKindOfClass:[NSString class]])
    {
        self.imageView.image = IMAGE(model);
    }else if ([model isKindOfClass:[UIImage class]])
    {
        self.imageView.image = model;
    }
    
}

@end



@interface MCImageScrollView()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    
}

@end

@implementation MCImageScrollView
- (instancetype)initWithClass:(Class)class scrollDirection:(UICollectionViewScrollDirection)scrollDirection
{
    self = [super init];
    if (self) {
        //UICollectionViewScrollDirection) {
        //        UICollectionViewScrollDirectionVertical,
        //        UICollectionViewScrollDirectionHorizontal
        //    };
        if (!class) {
            class = [imageViewCell class];
        }
        
        UICollectionView *cV = ({
            UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc]init];
            layou.scrollDirection =scrollDirection;
            UICollectionView*l=[[ UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height) collectionViewLayout:layou];
            [l registerClass:class forCellWithReuseIdentifier:cell];
            l.backgroundColor = self.backgroundColor;
            l.showsHorizontalScrollIndicator = NO;
            l.showsVerticalScrollIndicator = NO;
            l.dataSource = self;
            l.delegate =self;
            l.backgroundColor = self.backgroundColor;
            [self addSubview:l];
            l;
        });
        _collectionView = cV;
        
        [cV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
        
    }
    return self;
}
- (instancetype)initWithClass:(Class)class
{
    self = [super init];
    if (self) {
    
        //UICollectionViewScrollDirection) {
//        UICollectionViewScrollDirectionVertical,
//        UICollectionViewScrollDirectionHorizontal
//    };
        if (!class) {
            class = [imageViewCell class];
        }
        
        UICollectionView *cV = ({
            UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc]init];
            layou.scrollDirection =UICollectionViewScrollDirectionHorizontal;
            UICollectionView*l=[[ UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height) collectionViewLayout:layou];
            [l registerClass:class forCellWithReuseIdentifier:cell];
            l.backgroundColor = self.backgroundColor;
            l.showsHorizontalScrollIndicator = NO;
            l.dataSource = self;
            l.delegate =self;
            l.backgroundColor = self.backgroundColor;
            [self addSubview:l];
            l;
        });
        _collectionView = cV;
        
        [cV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
        
    }
    return self;
}

-(void)updateViewForImages:(NSArray *)images
{
    [self.images removeAllObjects];
    [self.images addObjectsFromArray:images];
    [self.collectionView reloadData];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *viewCell =[collectionView dequeueReusableCellWithReuseIdentifier:cell forIndexPath:indexPath];
    id model = self.images[indexPath.row];
    [viewCell update:model collectionView:collectionView cellForItemAtIndexPath:indexPath];
    
    return viewCell;
}



#pragma mark -----代理方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.delegate respondsToSelector:@selector(imageScrollView:imageCellAtIndexPath:)]) {
    [self.delegate imageScrollView:collectionView imageCellAtIndexPath:indexPath];
    }
}

-(void)deselectItem
{
    for (NSIndexPath *indexPath in [self.collectionView indexPathsForSelectedItems]) {
        [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
    }

}








#pragma mark -----数据源方法

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
   
    
    return [self.dataSource imageScrollView:collectionView imageCellForSectionAtIndex:section];;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    
    return [self.dataSource imageScrollView:collectionView lineSpacingForSectionAtIndex:section];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
  return [self.dataSource imageScrollView:collectionView  interitemSpacingForSectionAtIndex:section];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [self.dataSource imageScrollView:collectionView sizeForItemAtIndexPath:indexPath];
}



-(NSMutableArray *)images
{
    if (!_images) {
        _images = [NSMutableArray array];
    }
    return _images;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
